# -*- coding: utf-8 -*-
# @Time : 2020-05-28 23:11
# @Author : 刘跃飞
# @Email : 136686029@qq.com
# @File : PyDateTime.py
# @Project : PyDateTime

import time
import datetime
import calendar


today = datetime.date.today()#今天日期
yesterday = today - datetime.timedelta(days=1) #昨天日期
tomorrow = today + datetime.timedelta(days=1)#明天日期

now = time.strftime("%Y-%m-%d %H:%M:%S")


def  timestamp_to_str(timestamp=None,format='%Y-%m-%d %H:%M:%S'):
    '''这个是把时间戳转换成格式化好的实际，如果不传时间戳，那么就返回当前的时间'''
    if timestamp:
        return  time.strftime(format,time.localtime(timestamp))

    else:
        return  time.strftime(format,time.localtime())


print(timestamp_to_str(1554307200,'%Y-%m-%d'))
print(timestamp_to_str(format='%Y-%m-%d'))
print(timestamp_to_str())

def str_to_timestamp(str=None,format='%Y-%m-%d %H:%M:%S'):
    # 格式化好的时间转时间戳的,如果不传格式化好的时间，就返回当前的时间戳
    if str:
        return int(time.mktime(time.strptime(str,format)))
    else:
        return int(time.time())

print(str_to_timestamp())
print(str_to_timestamp('2019-04-04 12:12:34'))
print(str_to_timestamp('2019-07-09','%Y-%m-%d'))



def get_current_week(x_time):
    '''
    求本周的星期一
    :param x_time:具体日期
    :return:当周星期一
    '''
    monday = datetime.datetime.strptime(x_time, "%Y-%m-%d")
    one_day = datetime.timedelta(days=1)
    while monday.weekday() != 0:
        monday -= one_day
    return monday.strftime('%Y-%m-%d')

print(get_current_week('2020-05-28'))


def dateRange(beginDate, endDate):
    '''
    获取时间段内所有天
    :param beginDate:
    :param endDate:
    :return:
    '''
    dates = []
    dt = datetime.datetime.strptime(beginDate, "%Y-%m-%d")
    date = beginDate[:]
    while date <= endDate:
        dates.append(date)
        dt = dt + datetime.timedelta(1)
        date = dt.strftime("%Y-%m-%d")
    return dates

print(dateRange('2019-11-25', '2019-12-03'))
# ['2019-11-25','2019-11-26','2019-11-27','2019-11-28','2019-11-29','2019-11-30','2019-12-01','2019-12-02','2019-12-03']


def weekRange(beginDate, endDate):
    '''
    时间段内每个星期的周一日期
    :param beginDate:
    :param endDate:
    :return:
    '''
    date_week = []
    date_list = dateRange(beginDate, endDate)
    for i in date_list:
        dt = datetime.datetime.strptime(i, "%Y-%m-%d")
        if dt.weekday() == 0:
            date_week.append(i)
    if date_week[0] != date_list[0]:
        date_week.insert(0,get_current_week(date_list[0]))

    return date_week

weekRange('2019-11-12', '2019-12-11')
# ['2019-11-11', '2019-11-18', '2019-11-25', '2019-12-02', '2019-12-09']


def get_current_month_start_and_end(date):
    """
    年份 date(2017-09-08格式)
    :param date:
    :return:本月第一天日期和本月最后一天日期
    """
    if date.count('-') != 2:
        raise ValueError('- is error')
    year, month = str(date).split('-')[0], str(date).split('-')[1]
    end = calendar.monthrange(int(year), int(month))[1]
    start_date = '%s-%s-01' % (year, month)
    end_date = '%s-%s-%s' % (year, month, end)
    return [start_date, end_date]

get_current_month_start_and_end('2020-05-28')